<?php
/**
 * Description of WebCar
 *
 * @author Marco Mounzer
 * @file WebCar
 * @copyright
 * $Id: WebCar $
 */
require_once 'Database.php';
require_once 'ObjectForm.php';
require_once 'WebTipologia.php';
require_once 'WebAlimentazione.php';
require_once 'WebMarca.php';
require_once 'WebPhoto.php';

class WebCar extends ObjectForm {

    protected $caption = 'Veicolo';
    public $Id;
    public $online;
    public $offerta;
    public $tipologia;
    public $marca;
    public $modello;
    public $anno;
    public $km;
    public $colore;
    public $cc;
    public $kw;
    public $alimentazione;
    public $portatatotale;
    public $portatautile;
    public $prezzo;
    public $iva;
    public $note;
    public $photo = 0;

    public function __construct() {
        $this->Id = 0;
        $this->tipologia = new WebTipologia();
        $this->marca = new WebMarca();
        $this->alimentazione = new WebAlimentazione();
        $this->photo = new WebPhoto();
    }

    protected $formFields = array('Id' => ObjectForm::TYPE_HIDDEN,
        'marca' => ObjectForm::TYPE_SELECT,
        'tipologia' => ObjectForm::TYPE_SELECT,
        'modello' => ObjectForm::TYPE_INPUT,
        'anno' => ObjectForm::TYPE_INPUT,
        'km' => ObjectForm::TYPE_INPUT,
        'colore' => ObjectForm::TYPE_INPUT,
        'cc' => ObjectForm::TYPE_INPUT,
        'kw' => ObjectForm::TYPE_INPUT,
        'alimentazione' => ObjectForm::TYPE_SELECT,
        'portatatotale' => ObjectForm::TYPE_INPUT,
        'portatautile' => ObjectForm::TYPE_INPUT,
        'prezzo' => ObjectForm::TYPE_INPUT,
        'iva' => ObjectForm::TYPE_BOOL,
        'online' => ObjectForm::TYPE_BOOL,
        'offerta' => ObjectForm::TYPE_BOOL,
        'note' => ObjectForm::TYPE_TEXT,
        'photo' => ObjectForm::TYPE_HIDDEN,
    );
    protected $formFieldsDescriptors = array('tipologia' => 'nome', 'alimentazione' => 'nome', 'marca' => 'nome');

    /**
     *
     * @param type $modulo
     * @param type $caption
     * @param type $descr_field
     * @param type $data
     * @param type $optgroup
     * @param type $where
     * @param type $button_new
     * @param type $show_price
     * @return string
     */
    public function ChooserDatabaseTable($modulo, $caption, $descr_field, $data = null, $optgroup = false, $where = null, $button_new = true, $button_update = true, $show_price = true) {
        //$name = strtolower(get_class($obj));
        $name = Database::getTableNameFromClassName($this);

        if (is_null($data))
            $data = Database::GetIstance()->loadObjects($this, $where, $descr_field);

        $count = count($data);
        if ($optgroup) {
            $count = 0;
            foreach ($data as $v) {
                $count+=count($v);
            }
        }

        global $uploaddir;
        //$uploaddir = $_SERVER["DOCUMENT_ROOT"] . "images/cars/";
        //$html = '<form action="" method="post" submit="return false;"><table style="width: 600px"><thead><tr><th colspan="5">' . ucfirst($caption) . ' [' . $count . ']</td></tr></thead>';

        $ordinamento = '<th>' . $count . ' Veicoli</th>
            <th style="text-align: left; white-space: nowrap; width: 55%;"><span id="modello_s"><a order="m_u" href=""><img style="height: 10px;" src="/images/ub.png" /></a> Modello <a order="m_d" href=""><img style="height: 10px;" src="/images/db.png" /></a></span></th>
            <th style="text-align: left; white-space: nowrap; width: 15%;"><span id="anno_s"><a order="a_u" href=""><img style="height: 10px;" src="/images/ub.png" /></a> Anno <a order="a_d" href=""><img style="height: 10px;" src="/images/db.png" /></a></span></th>
            <th style="text-align: left; white-space: nowrap; width: 15%;"><span id="km_s"><a order="k_u" href=""><img style="height: 10px;" src="/images/ub.png" /></a> Km <a order="k_d" href=""><img style="height: 10px;" src="/images/db.png" /></a></span></th>
            <th style="text-align: left; white-space: nowrap; width: 15%;"><span id="prezzo_s"><a order="p_u" href=""><img style="height: 10px;" src="/images/ub.png" /></a> Prezzo <a order="p_d" href=""><img style="height: 10px;" src="/images/db.png" /></a></span></th>';

        $html = '<form action="" method="post" submit="return false;"><table style="width: 800px"><thead><tr>' . $ordinamento . '</tr></thead>';

        $html .= '<tbody><tr></tr>
          <td colspan="5"></td>
          </tr>';

        foreach ($data as $v) {

            $onclick = "ChangeCenterPage('$modulo', '$name', $v->Id)";

            $img = $v->photo->Id . '.' . $v->photo->extension;
            if (file_exists($uploaddir . $img))
                $img = '<img style="max-height: 200px; max-width: 200px; border: 1px solid gray;" src="/images/cars/' . $img . '" />';
            else
                $img = '<img style="max-height: 200px; max-width: 200px; border: 1px solid gray;" src="/images/automoda.png" />';


            $html .= $this->row($onclick, $img, $v->modello, $v->anno, $v->prezzo, $v->km, $v->note, $show_price);
        }

        $html .= '</tbody>';

        if ($button_update || $button_new) {
            $html .= '<tfoot><tr><td colspan="5">';
            if ($button_update)
                $html .= '<button type="button" onclick="this.disabled = true; location.reload(true);">Aggiorna</button>';

            if ($button_new) {
                $button_new_obj = "ChangeCenterPage('$modulo', '$name', null)";
                $html .= '<button type="button" onclick="' . $button_new_obj . '">Nuovo</button>';
            }
            $html .= '</td></tr></tfoot>';
        }
        $html .= '</table></form>';
        return $html;
    }

    private function row($onclick, $img, $modello, $anno, $prezzo, $km, $varie, $show_price) {
        ob_start();
        ?>
        <tr style="cursor: pointer; font-size: 12px; border-bottom: 1px solid grey;" onclick="<?php echo $onclick; ?>">
            <td rowspan="2" style="vertical-align: top"><?php echo $img; ?></td>
            <td style="height: 15px; background-color: #dddddd;"><?php echo $modello; ?></td>
            <td style="height: 15px; background-color: #dddddd;"><?php echo $anno; ?></td>
            <td style="height: 15px; background-color: #dddddd;"><?php echo $km; ?></td>
            <td style="height: 15px; background-color: #dddddd; color: red; font-weight: bolder">
                <span style=""><?php echo $show_price ? '€ ' . $prezzo : '&nbsp;-'; ?></span></td>
        </tr>
        <tr style="cursor: pointer; border-bottom: 1px solid grey;" onclick="<?php echo $onclick; ?>">
            <td colspan="4" style="border: 0px solid red;"><?php echo nl2br(substr($varie, 0, min(strlen($varie), 150)) . ' ...'); ?>&nbsp;</td>
        </tr>
        <?php
        return ob_get_clean();
    }

}
